package com.animee.jiujiangdict;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.animee.jiujiangdict.bean.ChengyuBean;
import com.animee.jiujiangdict.db.DBManager;
import com.animee.jiujiangdict.utils.HttpUtils;
import com.animee.jiujiangdict.utils.MyGridView;
import com.animee.jiujiangdict.utils.PatternUtils;
import com.animee.jiujiangdict.utils.URLUtils;
import com.google.gson.Gson;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/* 成语详情界面*/
public class ChengyuInfoActivity extends AppCompatActivity {
    TextView ziTv1,ziTv2,ziTv3,ziTv4,pyTv,jsTv,fromTv,exampleTv,yufaTv,yinzhengTv,yinghanTv;
    private String chengyu;
    ImageView collectIv;  //收藏的图片view
    MyGridView tyGv,fyGv;
    List<String>tongyiList;
    List<String>fanyiList;
    boolean isCollect = false;   //表示页面收藏图片点击状态的标志位
    boolean isExist = false;  //表示当前成语是否在收藏表格当中存在
    ArrayAdapter<String>tyAdapter,fyAdapter;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what==1) {
                String json = (String) msg.obj;
                if (!TextUtils.isEmpty(json)) {
                    ChengyuBean cbean = new Gson().fromJson(json, ChengyuBean.class);
                    ChengyuBean.ResultBean bean = cbean.getResult();
                    bean.setChengyu(chengyu);
                    //插入数据到数据库
                    DBManager.insertCyToCyutb(bean);
                    // 展示数据到控件上
                    showDataToView(bean);
                }else{
                    ChengyuBean.ResultBean bean = DBManager.queryCyFromCyutb(chengyu);
                    if (bean!=null) {
                        showDataToView(bean);
                    }
                }
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chengyu_info);
        Intent intent = getIntent();
        chengyu = intent.getStringExtra("chengyu");  //接受当一个界面传递进来的数据
        initView();
        initAdapter();
        // 获取当前成语是否被收藏了
        isExist = DBManager.isExistCyuInCollcyutb(chengyu);
        isCollect = isExist;
        setCollIvStyle();
        try {
            String encodeStr = URLEncoder.encode(chengyu, "UTF-8");
            String chengyuurl = URLUtils.getChengyuurl(encodeStr);
            HttpUtils.loadData(chengyuurl,handler);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
    /* 根据收藏的状态，改变星星的颜色*/
    private void setCollIvStyle() {
        if (isCollect) {
            collectIv.setImageResource(R.mipmap.ic_collection_fs);
        }else{
            collectIv.setImageResource(R.mipmap.ic_collection);
        }
    }
    private void initAdapter() {
        tongyiList = new ArrayList<>();
        fanyiList = new ArrayList<>();
        tyAdapter = new ArrayAdapter<String>(this,R.layout.item_word_jslv,R.id.item_wordlv_tv,tongyiList);
        fyAdapter = new ArrayAdapter<String>(this,R.layout.item_word_jslv,R.id.item_wordlv_tv,fanyiList);
        tyGv.setAdapter(tyAdapter);
        fyGv.setAdapter(fyAdapter);
    }


    private void initView() {
        ziTv1 = findViewById(R.id.chengyu_tv_zi1);
        ziTv2 = findViewById(R.id.chengyu_tv_zi2);
        ziTv3 = findViewById(R.id.chengyu_tv_zi3);
        ziTv4 = findViewById(R.id.chengyu_tv_zi4);
        pyTv = findViewById(R.id.chengyu_tv_py);
        jsTv = findViewById(R.id.chengyu_tv_js);
        fromTv = findViewById(R.id.chengyu_tv_from);
        exampleTv = findViewById(R.id.chengyu_tv_example);
        yufaTv = findViewById(R.id.chengyu_tv_yufa);
        yinzhengTv = findViewById(R.id.chengyu_tv_yinzheng);
        yinghanTv = findViewById(R.id.chengyu_tv_yinghan);
        tyGv = findViewById(R.id.chengyu_gv_tongyi);
        fyGv = findViewById(R.id.chengyu_gv_fanyi);
        collectIv = findViewById(R.id.chengyu_iv_collect);
    }

    /*
    * 将获取到的数据   展示在控件上
    * */
    private void showDataToView(ChengyuBean.ResultBean bean) {
        String chengyu = bean.getChengyu();
        ziTv1.setText(String.valueOf(chengyu.charAt(0)));
        ziTv2.setText(String.valueOf(chengyu.charAt(1)));
        ziTv3.setText(String.valueOf(chengyu.charAt(2)));
        ziTv4.setText(String.valueOf(chengyu.charAt(3)));
        pyTv.setText("拼音 : "+bean.getPinyin());
        jsTv.setText(bean.getChengyujs());
        fromTv.setText(bean.getFrom_());
        exampleTv.setText(bean.getExample());
        yufaTv.setText(bean.getYufa());
        yinzhengTv.setText(bean.getYinzhengjs());
        String ciyujs = bean.getCiyujs();
        if (!TextUtils.isEmpty(ciyujs)) {
            ciyujs = ciyujs.replace("]","\n").replace("[","").replace("∶","");
            yinghanTv.setText(ciyujs);
        }
        List<String> tList = bean.getTongyi();
        if (tList!=null&&tList.size()!=0) {
            tongyiList.addAll(tList);
            tyAdapter.notifyDataSetChanged();
        }

        List<String> fList = bean.getFanyi();
        if (fList!=null&&fList.size()!=0) {
            fanyiList.addAll(fList);
            fyAdapter.notifyDataSetChanged();
        }

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.chengyu_iv_back:
                finish();
                break;
            case R.id.chengyu_iv_collect:
                isCollect = !isCollect;
                setCollIvStyle();   //改完状态设置收藏按钮的颜色
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isExist&&!isCollect) {
            DBManager.deleteCyuToCollcyutb(chengyu);
        }
        if (!isExist&&isCollect) {
            DBManager.insertCyuiToCollcyutb(chengyu);
        }
    }
}
